package org.ourgrid.worker.business.requester;

import java.util.ArrayList;
import java.util.List;

import org.ourgrid.common.internal.IResponseTO;
import org.ourgrid.worker.business.controller.WorkerController;
import org.ourgrid.worker.request.ExecutionResultRequestTO;
import org.ourgrid.worker.response.HereIsGridProcessResultMessageHandleResponseTO;

public class ExecutionResultRequester extends AbstractWorkerExecutionClientRequester<ExecutionResultRequestTO> {

	public List<IResponseTO> execute(ExecutionResultRequestTO request) {
		List<IResponseTO> responses = new ArrayList<IResponseTO>();
		
		
		String workerClient = getWorkerClientAddress();
		
		if (workerClient != null) {
			HereIsGridProcessResultMessageHandleResponseTO to = new HereIsGridProcessResultMessageHandleResponseTO(request.getResult(),
					getWorkerStatusDAO().getConsumerAddress());
			to.setClientAddress(workerClient);
			
			responses.add(to);
		}
		
		if (getWorkerStatusDAO().isAllocatedForRemotePeer()) {
			WorkerController.getInstance().finishWorkAccounting();
		}
		
		setExecutionAsFinished(true);		
		
		return responses;
	}

}
